package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.pagebean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import java.util.List; // 修改这里
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

    public class BrandServiceimpl implements BrandService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() { // 修改这里
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll(); // 修改这里
        sqlSession.close();

        return brands;
    }

        @Override
        public void add(Brand brand) {
            SqlSession sqlSession = factory.openSession();
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
            mapper.add(brand);
            sqlSession.commit();
            sqlSession.close();
        }
        @Override
        public void deleteByIds(int[]ids){
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();

        }

        @Override
        public pagebean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;
        List<Brand> rows = mapper.selectByPage(begin, size);
        int totalCount = mapper.selectTotalCount();

        pagebean<Brand> pagebean = new pagebean<>();
        pagebean.setRows(rows);
        pagebean.setTatalcount(totalCount);

        sqlSession.close();
        return pagebean;

        }

        @Override
        public pagebean<Brand> selectByPageandCondition(int currentPage, int pageSize, Brand brand) {
            SqlSession sqlSession = factory.openSession();
            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

            int begain = (currentPage - 1) * pageSize;

            String brandName = brand.getBrandName();
            if (brandName != null && brandName.length() > 0) {
                brand.setBrandName("%" + brandName + "%");
            }

            String companyName = brand.getCompanyName();
            if (companyName != null && companyName.length() > 0) {
                brand.setCompanyName("%" + companyName + "%");
            }

            List<Brand> rows = mapper.selectByPageandCondition(begain, pageSize, brand);
            int totalCount = mapper.selectTotalCountByCondition(brand);

            pagebean<Brand> pageBean = new pagebean<>();
            pageBean.setRows(rows);
            pageBean.setTatalcount(totalCount);

            sqlSession.close();
            return pageBean;
        }
    }
